/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgMetodoPyramid.pkgPersistencia;

import javax.swing.JOptionPane;
import pkgMetodoPyramid.pkgSoporte.Constants;
import pkgUtil.Message;
import pkgUtil.PASConstants;
import pkgUtil.pkgSupportFile.ReadPASFileException;
import pkgUtil.pkgSupportFile.SavePASFileException;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.DialogSelectFile;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.SelectFile;

/**
 *
 * @author Administrador
 */
public class GestorFilePyramid {

    public static PASPyramidFile grabarFilePyramid(java.awt.Component plla, BDataPASPyramidFile dataFile) throws SavePASFileException {
        SelectFile selectFile = DialogSelectFile.showDialogFileForSave(plla, Constants.DESCRIPTION_FILE_EXTENSION, PASConstants.FILE_EXTENSION);

        if (selectFile.getResultSelection() == DialogSelectFile.ERROR_OPTION) {
            throw new SavePASFileException();
        }

        PASPyramidFile pasFilePyramid = new PASPyramidFile(selectFile.getSelectFile());

        if (validCancelOperation(selectFile, pasFilePyramid)) {
            return pasFilePyramid;
        }

        if (!deleteExistsFile(selectFile)) {
            pasFilePyramid.cancelOperation();
            return pasFilePyramid;
        }

        HeadDataPASPyramidFile head = getInstanceHead(dataFile);
        pasFilePyramid.setHeadData(head);
        pasFilePyramid.setReqRespPyramid(dataFile.getReqRespPyramid());

        pasFilePyramid.validForSave();
        if (pasFilePyramid.isValid()) {
            pasFilePyramid.save();
        }
        return (PASPyramidFile) pasFilePyramid;
    }

    public static PASPyramidFile leerFilePyramid(java.awt.Component plla) throws ReadPASFileException {
        SelectFile selectFile = DialogSelectFile.showDialogFileForRead(plla, Constants.DESCRIPTION_FILE_EXTENSION, PASConstants.FILE_EXTENSION);

        if (selectFile.getResultSelection() == DialogSelectFile.ERROR_OPTION) {
            throw new ReadPASFileException();
        }

        PASPyramidFile pasFilePyramid = new PASPyramidFile(selectFile.getSelectFile());

        if (validCancelOperation(selectFile, pasFilePyramid)) {
            return pasFilePyramid;
        }

        pasFilePyramid.validForRead();
        if (pasFilePyramid.isValid()) {
            pasFilePyramid.read();
        }

        if (!validTypeMethod(pasFilePyramid)) {
            pasFilePyramid.cancelOperation();
        }

        return (PASPyramidFile) pasFilePyramid;
    }

    private static HeadDataPASPyramidFile getInstanceHead(BDataPASPyramidFile dataFile) {
        HeadDataPASPyramidFile head = new HeadDataPASPyramidFile();
        head.setTotalVarones(dataFile.getTotalVarones());
        head.setTotalMujeres(dataFile.getTotalMujeres());
        head.setTotal(dataFile.getTotal());

        if (dataFile.getTitulo() == null) {
            dataFile.setTitulo(Constants.TITULODEFAULT);
        }
        if (dataFile.getSubTitulo() == null) {
            dataFile.setSubTitulo(Constants.SUBTITULODEFAULT);
        }

        head.setTitulo(dataFile.getTitulo());
        head.setSubTitulo(dataFile.getSubTitulo());

        return head;
    }

    private static boolean validCancelOperation(SelectFile selectFile, PASPyramidFile pasFilePyramid) {
        if (selectFile.getResultSelection() == DialogSelectFile.CANCEL_OPTION) {
            pasFilePyramid.cancelOperation();
        }
        return pasFilePyramid.isCancelOperation();
    }

    private static boolean deleteExistsFile(SelectFile selectFile) {
        int resSelect;
        if (selectFile.existsFile()) {
            resSelect = Message.mensaje_Consulta_YES_NO_OPTION("El nombre del archivo ya existe. ¿Desea reemplazarlo?", "Mensaje", Message.TIPO_INTERROGACION);
            if (resSelect == JOptionPane.YES_OPTION) {
                return selectFile.deleteFile();
            } else {
                return false;
            }
        }
        return true;
    }

    public static boolean validTypeMethod(PASPyramidFile pasFilePyramid) {
        if (pasFilePyramid.getHeadData().getIdentificadorMethod() != PASConstants.ID_FILE_METHOD_PYRAMID) {
            Message.mensaje_Estandar("El archivo seleccionado no pertenece al método Pyramid", "Mensaje", Message.TIPO_ERROR);
            return false;
        }
        return true;

    }
}
